package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class RoleHandler {

//    @Autowired
//    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleService roleService;


    @RequestMapping("/role/remove/by/role/id/array.json")
    @ResponseBody
    public ResultEntity<String> removeRole(@RequestBody List<Integer> roleIdList){
//        System.out.println(roleIdList);

        roleService.deleteRole(roleIdList);

        return ResultEntity.successWithoutData();
    }
    @RequestMapping("/role/update.json")
    @ResponseBody
    public ResultEntity<String> updateRole(Role role){
        System.out.println(role.getName());

        roleService.updateRole(role);

        return ResultEntity.successWithoutData();

    }


    @RequestMapping("/role/save.json")
    @ResponseBody
    public ResultEntity<String> sveRole(Role role){


        roleService.saveRole(role);

        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/role/get/page/info.json")
    @ResponseBody
    public ResultEntity<PageInfo<Role>> getPageInfo(

            //默认值是1
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword",defaultValue = "") String keyword){


        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
//        System.out.println(pageInfo.getList().get(0).getId());
//        System.out.println(pageInfo.getList().size());

        //封装到ResultEntity对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }
}
